//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props:{item:{}},
    template:[
        '<li :class="\'li\'+item.icon">',
        '   <a :class="item.icon" v-if="item.type === 0" href="javascript:;">',
        '       <i v-if="item.icon != null" :class="item.icon"></i>',
        '       <span>{{item.name}}</span>',
        '       <i class="fa fa-angle-left pull-right"></i>',
        '   </a>',
        '   <ul v-if="item.type === 0" class="treeview-menu">',
        '       <menu-item :item="item" v-for="item in item.list"></menu-item>',
        '   </ul>',
        
        '   <a :class="item.icon" v-if="item.type === 3" :href="\'#\'+item.url" onclick="sysMsgNum()" >',
        '       <i v-if="item.icon != null" :class="item.icon"></i>',
        '       <span>{{item.name}}</span>',
        '   </a>',

        '   <a :class="item.icon" v-if="item.type === 1 && item.parentId === \'0\' && item.menuId!=\'76\'" :href="\'#\'+item.url" onclick="sysMsgNum()" >',
        '       <i v-if="item.icon != null" :class="item.icon"></i>',
        '       <span>{{item.name}}</span>',
        '   </a>',

        '   <a v-if="item.type === 1 && item.parentId != 0" :href="\'#\'+item.url" onclick="sysMsgNum()" > {{item.name}}</a>',
        '</li>'
    ].join('')
});

//点击除菜单外的其他地方菜单收缩
$(function(){
//    $("body").mouseout(function(e){
//       var target =  $(e.target);
//       if(target.is(".main-sidebar") || target.is("ul.sidebar-menu") || target.is("span") || target.is("a") || target.is("section.sidebar")){
//       }
//       else{
//           $("body").addClass('sidebar-collapse').trigger('collapsed.pushMenu');
//       }
//    });
//    //iframe内容点击事件
//    $("iframe").load(function(){
//        $("iframe").contents().find("body").mouseout(function(){
//            $("body").addClass('sidebar-collapse').trigger('collapsed.pushMenu');
//        }); 
//    }); 
});

//iframe自适应
$(window).on('resize', function() {
    var $content = $('.content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function() {
        $(this).height($content.height());
    });
}).resize();

//注册菜单组件
Vue.component('menuItem',menuItem);

var vm = new Vue({
    el:'#rrapp',
    data:{
        user:{},
        menuList:{},
        main:"modules/business/homepage/homepage.html",
        password:'',
        newPassword:'',
        navTitle:"首页    ",
        tableData: [], // 获取表格数据
        pageNo: 1, // 当前页
        totalPage: 0, // 总页数
        pageSize: 10, // 每页数据条数
    },
    methods: {
        getMenuList: function (event) {
            $.getJSON("sys/menu/nav?_"+$.now(), function(r){
                /*console.log(r.menuList,'菜单列表================= 在这里修改菜单按钮')
                for(var i in r.menuList){
                    r.menuList[i].icon='fa fa-cog'
                }*/
                vm.menuList = r.menuList;
            });
        },
        getUser: function(){
            $.getJSON("sys/user/info?_"+$.now(), function(r){
                vm.user = r.user;
            });
        },
        updatePassword: function(){
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "修改密码",
                area: ['550px', '270px'],
                shadeClose: false,
                content: jQuery("#passwordLayer"),
                btn: ['修改','取消'],
                btn1: function (index) {
                    var data = "password="+vm.password+"&newPassword="+vm.newPassword;
                    $.ajax({
                        type: "POST",
                        url: "sys/user/password",
                        data: data,
                        dataType: "json",
                        success: function(result){
                            if(result.code == 0){
                                layer.close(index);
                                layer.alert('修改成功', function(index){
                                    location.reload();
                                });
                            }else{
                                layer.alert(result.msg);
                            }
                        }
                    });
                }
            });
        },
        donate: function () {
            layer.open({
                type: 2,
                title: false,
                area: ['806px', '467px'],
                closeBtn: 1,
                shadeClose: false,
                content: ['http://cdn.renren.io/donate.jpg', 'no']
            });
        },
        // 获取数据
        getData: function() {
            // 接口获取数据后对baseForm对象中对应的字段赋值，字段已经绑定页面对应的表单部分
            $.ajax({
                type: "GET",
                url: 'business/sysmessage/list',
                contentType: "application/json",
                success: function (res) {debugger
                    if (res.code === '0') {
                        vm.tableData = res.page.list
                        vm.totalPage = res.page.totalPage
                        vm.pangeNo = res.page.currPage
                    } else {
                        alert(res.msg);
                    }
                }
            });
        },
        // 分页
        pageChange: function () {
            var params = {
                'searchValue': vm.findKey,
                'page': vm.pageNo
            };
            $.ajax({
                type: "GET",
                url: 'business/sysmessage/list',
                contentType: "application/json",
                success: function (res) {
                    if (res.code === '0') {
                        vm.tableData = res.page.list;
                        vm.totalPage = res.page.totalPage;
                        vm.pangeNo = res.page.currPage;
                    } else {
                        alert(res.msg);
                    }
                }
            });
        },
    },
    created: function(){
        this.getMenuList();
        this.getUser();
        sysMsgNum();
    },
    updated: function(){
        //路由
        var router = new Router();
        routerList(router, vm.menuList);
        router.start();
    }
});



function routerList(router, menuList){
    for(var key in menuList){
        var menu = menuList[key];
        if(menu.type == 0){
            routerList(router, menu.list);
        }else if(menu.type == 1){
            router.add('#'+menu.url, function() {
                var url = window.location.hash;
                
                //替换iframe的url
                vm.main = url.replace('#', '');
                
                //导航菜单展开
                $(".treeview-menu li").removeClass("active");
                $("a[href='"+url+"']").parents("li").addClass("active");
                
                vm.navTitle = $("a[href='"+url+"']").text();
            });
        }
    }
}

function sysMsgNum(){
    $.ajax({
        type: "POST",
        url: "business/sysmessage/getNum",
        data: {
            readFlag:'0'
        },
        dataType: "json",
        success: function(result){
            if(result.code == 0){
                $("#topSystemMsgNum").html(result.data);
            }else{
                $("#topSystemMsgNum").html(0);
            }
        }
    });
}

function openSysMsg(){
    pageii = layer.open({
        title : "系统消息",
        type : 2,
        area : [ "80%", "85%"],
        content : "modules/business/xtxx.html",
        end : function () {sysMsgNum()}
    });
}

function alwayUseHref(url){debugger
    location.href='../../../index.html#' + url;
}